/**
 * PLM项目服务配置入口
 * 统一导出项目中的服务模块
 */

// 导入标准服务
const services = {};

// 项目服务导入
try {
  services.researchService = require('../services/research_service');
} catch (e) {
  console.warn('导入模块失败: ../services/research_service，使用空实现', e);
  services.researchService = {};
}

try {
  services.projectService = require('../services/project_service');
} catch (e) {
  console.warn('导入模块失败: ../services/project_service，使用空实现', e);
  services.projectService = {};
}

try {
  services.resourceService = require('../services/resource_service');
} catch (e) {
  console.warn('导入模块失败: ../services/resource_service，使用空实现', e);
  services.resourceService = {};
}

try {
  services.flowService = require('../services/flow_service');
} catch (e) {
  console.warn('导入模块失败: ../services/flow_service，使用空实现', e);
  services.flowService = {};
}

try {
  services.projectStateService = require('../services/project_state_service');
} catch (e) {
  console.warn('导入模块失败: ../services/project_state_service，使用空实现', e);
  services.projectStateService = {};
}

try {
  services.branchService = require('../services/branch_service');
} catch (e) {
  console.warn('导入模块失败: ../services/branch_service，使用空实现', e);
  services.branchService = {};
}

// 导入外部模拟服务
let plmMockService = null;
try {
  plmMockService = require('../services/plm_mock_service');
  console.log('成功导入外部PLM模拟服务');
  
  // 包装getProjectList方法，确保直接返回结果而不是Promise
  const originalGetProjectList = plmMockService.getProjectList;
  plmMockService.getProjectList = function(params) {
    console.log('[services_config] 为PLM项目列表请求返回null，使用真实云端数据');
    
    // 返回null，强制系统使用真实云端数据
    return null;
  };
} catch (e) {
  console.warn('导入外部PLM模拟服务失败，将使用简化的模拟服务', e);
  
  // 使用简化的Mock服务作为备选
  plmMockService = {
    // 获取项目列表 - 返回直接结果而不是Promise
    getProjectList: (params = {}) => {
      console.warn('使用简化的模拟服务getProjectList，请配置完整的模拟服务');
      
      return {
        code: 0,
        msg: 'ok',
        list: [
          {
            _id: 'backup-01',
            model: '备用项目-1',
            cate: '备用类型',
            status: 1,
            devManager: '未指定',
            bizManager: '未指定',
            createTime: Math.floor(Date.now() / 1000) - 86400,
            updateTime: Math.floor(Date.now() / 1000),
            description: '这是一个备用项目，由简化模拟服务提供',
            progress: 0,
            priority: 'normal'
          },
          {
            _id: 'backup-02',
            model: '备用项目-2',
            cate: '备用类型',
            status: 2,
            devManager: '未指定',
            bizManager: '未指定',
            createTime: Math.floor(Date.now() / 1000) - 86400 * 2,
            updateTime: Math.floor(Date.now() / 1000) - 3600,
            description: '这是另一个备用项目，由简化模拟服务提供',
            progress: 15,
            priority: 'high'
          }
        ],
        total: 2,
        page: params.page || 1,
        size: params.size || 20
      };
    },
    
    // 获取项目详情 - 返回直接结果而不是Promise
    getProjectDetail: (id) => {
      console.warn('使用简化的模拟服务getProjectDetail，请配置完整的模拟服务');
      
      return {
        code: 0,
        msg: 'ok',
        data: {
          _id: id || 'backup-detail',
          model: '备用项目详情',
          cate: '备用类型',
          status: 1,
          devManager: '未指定',
          bizManager: '未指定',
          createTime: Math.floor(Date.now() / 1000) - 86400,
          updateTime: Math.floor(Date.now() / 1000),
          description: '这是一个备用项目详情，由简化模拟服务提供',
          progress: 0,
          priority: 'normal'
        }
      };
    }
  };
}

// 将模拟服务添加到services对象中
services.plmMockService = plmMockService;

// 在开发环境下合并其他模拟服务
if (process.env.NODE_ENV === 'development' || typeof wx === 'undefined') {
  // 可以添加其他开发环境特有的服务
}

const formConfigs = {
  default: {
    fields: [
      { name: 'model', label: '型号', type: 'string', required: true },
      { name: 'projectType', label: '项目类型', type: 'string', required: true },
      { name: 'customerName', label: '客户名称', type: 'string', required: true },
      { name: 'startDate', label: '开始日期', type: 'date', required: true },
      { name: 'deadline', label: '截止日期', type: 'date', required: true },
      { name: 'priority', label: '优先级', type: 'string', required: true },
      { name: 'description', label: '项目描述', type: 'string', required: false },
      { name: 'level1', label: '一级状态', type: 'int', required: true },
      { name: 'level2', label: '二级状态', type: 'string', required: true },
      { name: 'level3', label: '三级状态', type: 'string', required: false },
      { name: 'level4', label: '四级状态', type: 'string', required: false },
      { name: 'branchId', label: '分支ID', type: 'string', required: true },
      { name: 'branchName', label: '分支名称', type: 'string', required: true },
      { name: 'completionPercentage', label: '项目完成度', type: 'int', required: true },
      { name: 'developmentStatus', label: '开发状态', type: 'string', required: true },
      { name: 'developmentManager', label: '研发负责人', type: 'string', required: false },
      { name: 'businessManager', label: '业务负责人', type: 'string', required: false },
      // ...可继续补充扩展字段，参照你的映射表...
    ]
  }
  // 可扩展其他类型表单配置
};

const ProjectDataBiz = {
  getFormConfigByType(type) {
    return formConfigs[type] || formConfigs['default'];
  }
};

// 项目服务
let ProjectService = {};
let ResearchProjectService = {};
let ProjectFormConfig = {};

try {
  ProjectService = require('../services/project_service.js');
} catch (e) {
  console.warn('导入模块失败: ../services/project_service.js，使用空实现', e);
}

try {
  ResearchProjectService = require('../services/research_project_service.js');
} catch (e) {
  console.warn('导入模块失败: ../services/research_project_service.js，使用空实现', e);
}

try {
  ProjectFormConfig = require('../comm/biz/project_form_config.js');
} catch (e) {
  console.warn('导入模块失败: ../comm/biz/project_form_config.js，使用空实现', e);
}

// 扩展ProjectService，确保包含getProjectFormConfig方法和验证保护参数的方法
if (ProjectService && typeof ProjectService === 'object') {
  ProjectService.ensureProtectionSubParams = function(data) {
    // 调用服务中的验证保护参数方法
    if (typeof ProjectService.validateProtectionParams === 'function') {
      return ProjectService.validateProtectionParams(data);
    }
    return data;
  };
}

// 导出所有服务
module.exports = services;